package org.ceteca.explica.client.partners;

import java.util.HashSet;

import org.ceteca.explica.client.gui.common.CommonClient;
import org.ceteca.explica.core.common.BaseVO;
import org.ceteca.explica.core.common.MultiSelectionSubentityOT;
import org.ceteca.explica.core.common.SelectionSubentityOT;
import org.ceteca.explica.core.partners.ContactOT;
import org.ceteca.explica.core.partners.DiscountOT;
import org.ceteca.explica.core.partners.InterfacePartners;
import org.ceteca.explica.core.partners.PartnerGroupOT;
import org.ceteca.explica.core.util.Converter;
import org.ceteca.explica.core.util.OperationResult;
import org.ceteca.explica.core.warehouse.InterfaceWarehouse;
import org.ceteca.explica.core.warehouse.ProductGroupOT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Text;




/**
 * @author Juan C. Díaz (juancarlosdiaz@ceteca.net) <br>
 *         Copyright (c) 2009 Fundación Centro Tecnolóxico da Carne
 * 
 * <br>
 * <br>
 *         This program is free software: you can redistribute it and/or modify
 *         it under the terms of the GNU General Public License as published by
 *         the Free Software Foundation, either version 3 of the License, or (at
 *         your option) any later version. <br/>
 *         This program is distributed in the hope that it will be useful, but
 *         WITHOUT ANY WARRANTY 
 *         <br>without even the implied warranty of
 *         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *         General Public License for more details. <br/>
 *         You should have received a copy of the GNU General Public License
 *         along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 */
public class DiscountClient extends CommonClient {

	private Text entryCode,entryDescription, entryLowerLimit, entryUpperLimit, entryPercentaje, entryVersion, entryProductGroup, entryPartnerGroup;
	private Button buttonProductGroup, buttonPartnerGroup;
	
	
	


	public DiscountClient(){
		super(DiscountOT.class, InterfacePartners.EXPLICA_EDIT_DISCOUNT_GLADE_PATH, 
				InterfacePartners.EDIT_DISCOUNT_DIALOG_NAME);
	}

	@Override
	public void buildSelectionSets() {
		lSelectionSubentities = new HashSet<SelectionSubentityOT>();
		lSelectionSubentities.add(new SelectionSubentityOT(
				InterfacePartners.EXPLICA_ENTITY_PARTNERGROUP_ID,
				InterfacePartners.EDIT_PARTNER_GROUP_WIDGETNAME,
				this, buttonPartnerGroup));
		lSelectionSubentities.add(new SelectionSubentityOT(
				InterfaceWarehouse.EXPLICA_ENTITY_PRODUCTGROUP_ID,
				InterfacePartners.EDIT_PRODUCT_GROUP_WIDGETNAME,
				this, buttonProductGroup));
		lMultiSelectionSubentities = new HashSet<MultiSelectionSubentityOT>();


	}

	@Override
	public void build_extension_fields_view() {
		// TODO Auto-generated method stub

	}

	@Override
	public BaseVO getEntityData() {
		DiscountOT entity = new DiscountOT();

		if (this.entEditView != null) {
			this.getWidgets();
			entity.setDescription(Converter.getInstance().toString(entryDescription.getText()));
			entity.setLowerLimit(Converter.getInstance().parseFloat(
					entryLowerLimit.getText()));
			entity.setUpperLimit(Converter.getInstance().parseFloat(
					entryUpperLimit.getText()));
			entity.setPercentage(Converter.getInstance().parseFloat(
					entryPercentaje.getText()));
			entity.setProductGroup((ProductGroupOT) (entryProductGroup
					.getData()));
			entity.setPartnerGroup((PartnerGroupOT) (entryPartnerGroup
					.getData()));
			if (selectedEntity != null) {
				entity.setVersion(((DiscountOT) this.selectedEntity)
						.getVersion());
				entity
						.setIdDiscount(((DiscountOT) this.selectedEntity)
								.getIdDiscount());
			}
		}

		return entity;
	}

	@Override
	public void getWidgets() {
		
		int allOk = 0;
		// Entries
		entryCode = (Text) this.getWidget(
				InterfacePartners.EDIT_CODE_WIDGETNAME);
		if (entryCode == null)
			allOk = 1;
		entryDescription = (Text) this.getWidget(
				InterfacePartners.EDIT_DESCRIPTION_WIDGETNAME);
		if (entryDescription == null)
			allOk = 2;
		entryLowerLimit = (Text) this.getWidget(
				InterfacePartners.EDIT_DISCOUNT_LOWERLIMIT_WIDGETNAME);
		if (entryLowerLimit == null)
			allOk = 3;
		entryUpperLimit = (Text) this.getWidget(
				InterfacePartners.EDIT_DISCOUNT_UPPERLIMIT_WIDGETNAME);
		if (entryUpperLimit == null)
			allOk = 4;
		entryPercentaje = (Text) this.getWidget(
				InterfacePartners.EDIT_DISCOUNT_RATE_WIDGETNAME);
		if (entryPercentaje == null)
			allOk = 5;
		entryProductGroup = (Text) this.getWidget(
				InterfacePartners.EDIT_PRODUCT_GROUP_WIDGETNAME);
		if (entryProductGroup == null)
			allOk = 6;
		entryPartnerGroup = (Text) this.getWidget(
				InterfacePartners.EDIT_PARTNER_GROUP_WIDGETNAME);
		if (entryPartnerGroup == null)
			allOk = 7;
		
	

		if ((entryVersion = ((Text) this.getWidget(
				InterfacePartners.EDIT_VERSION_WIDGETNAME))) == null) {
			allOk = 18;
			this.logger.error("Error getting entry Version Widget:"
					+ InterfacePartners.EDIT_VERSION_WIDGETNAME);

		}

		buttonPartnerGroup = (Button) this
				.getWidget(
						InterfacePartners.EDIT_PARTNER_GROUP_SELECT_WIDGETNAME);
		if (buttonPartnerGroup == null)
			allOk = 19;

		buttonProductGroup = (Button) this.getWidget(
				InterfacePartners.EDIT_PRODUCT_GROUP_SELECT_WIDGETNAME);
		if (buttonProductGroup == null)
			allOk = 20;




		if (allOk != 0) {
			// this.logger.fatal("Error getting widgets");
			this.logger.debug("Error getting widget #" + allOk);
			fireExceptionAndExit("Error obtentiendo widgets");
			// System.exit(-1);
		}



	}

	@Override
	public void init_specific_components() {
		// TODO Auto-generated method stub

	}

	@Override
	public void setEditionMode(boolean enabled) {
		getWidgets();
		entryCode.setEnabled(false);
		entryDescription.setEnabled(enabled);
		entryPartnerGroup.setEnabled(enabled);
		entryProductGroup.setEnabled(enabled);
		entryLowerLimit.setEnabled(enabled);
		entryUpperLimit.setEnabled(enabled);
		entryPercentaje.setEnabled(enabled);
		buttonPartnerGroup.setEnabled(enabled);
		buttonProductGroup.setEnabled(enabled);
		entryVersion.setEnabled(false);
		
		if (isDetail()) {
			if (this.entityDetailView.getSubentityData().getParentEntity()
					.getEntity().getKey().equals(
							InterfacePartners.EXPLICA_ENTITY_PARTNERGROUP_ID)) {
				entryPartnerGroup.setEnabled(false);
				buttonPartnerGroup.setEnabled(false);
			}
			if (this.entityDetailView.getSubentityData().getParentEntity()
					.getEntity().getKey().equals(
							InterfaceWarehouse.EXPLICA_ENTITY_PRODUCTGROUP_ID)) {
				entryProductGroup.setEnabled(false);
				buttonProductGroup.setEnabled(false);
			}
		}

	}

	@Override
	public void setWidgetsValues(BaseVO entity) {

		DiscountOT ent = (DiscountOT) entity;
		getWidgets();

		entryCode.setText(Converter.getInstance().toString(
				ent.getIdDiscount()));
		entryDescription.setText(ent.getDescription() == null ? ""
				: ent.getDescription());
		entryPercentaje.setText(Converter.getInstance()
				.toString(ent.getPercentage()));
		entryLowerLimit.setText(Converter.getInstance().toString(
				ent.getLowerLimit()));
		entryUpperLimit.setText(Converter.getInstance().toString(
				ent.getUpperLimit()));
		entryVersion.setText(Converter.getInstance().toString(((DiscountOT)entity).getVersion()));		
		if (ent.getPartnerGroup() != null) {
			entryPartnerGroup.setText(ent.getPartnerGroup().getDescription());
			entryPartnerGroup.setData(ent.getPartnerGroup());
		}
		if (ent.getProductGroup() != null) {
			entryProductGroup.setText(ent.getProductGroup().getDescription());
			entryProductGroup.setData(ent.getProductGroup());
		}
	}

	@Override
	public OperationResult validateEntity() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void fillEntityData() {
		if (this.selectedEntity != null && this.entEditView != null) {
			getWidgets();
			setWidgetsValues((DiscountOT) selectedEntity);
		}
	}
}
